package com.wxy.xr.servlet;

import com.wxy.xr.bean.Company;
import com.wxy.xr.bean.Project;
import com.wxy.xr.bean.UploadParams;
import com.wxy.xr.service.CompanyService;
import com.wxy.xr.service.UserService;
import com.wxy.xr.service.WebsiteService;
import com.wxy.xr.service.impl.CompanyServiceImpl;
import com.wxy.xr.service.impl.UserServiceImpl;
import com.wxy.xr.service.impl.WebsiteServiceImpl;
import com.wxy.xr.util.Uploads;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/project/*")
public class ProjectServlet extends BaseServlet<Project> {
    private CompanyService companyService=new CompanyServiceImpl();
    private UserService userService=new UserServiceImpl();
    private WebsiteService websiteService=new WebsiteServiceImpl();
    public void front(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("projects",service.list());
        request.setAttribute("user",userService.list().get(0));
        request.setAttribute("footer",websiteService.list().get(0).getFooter());
        forward(request, response,"front/project.jsp");
    }
    public void admin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("projects",service.list());
        request.setAttribute("companys",companyService.list());
        forward(request, response,"admin/project.jsp");
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        UploadParams uploadParams = Uploads.parseRequest(request);

        Project project=new Project();

        BeanUtils.populate(project,uploadParams.getParams());

        //对公司信息特殊处理
        Company company = new Company();
        company.setId(Integer.valueOf(uploadParams.getParam("companyId").toString()));
        project.setCompany(company);

        FileItem item=uploadParams.getFileParam("imageFile");
        project.setImage(Uploads.uploadImage(item,request,project.getImage()));
        if(service.save(project)){
            redirect(request, response,"project/admin");
        } else {
            forward(request, response,"项目经验保存失败");
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] idstrs=request.getParameterValues("id");
        List<Integer> ids=new ArrayList<>();
        for(String idstr:idstrs){
            ids.add(Integer.valueOf(idstr));
        }

        if(service.remove(ids)){
            redirect(request, response,"project/admin");
        }else{
            forward(request, response,"项目经验删除失败");
        }
    }
}